package com.txf.simple.ui.activity;

import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.txf.simple.R;
import com.txf.simple.tools.Logger;

import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class RxJavaActivity extends AbsBaseActivity {
    @Override
    protected Drawable buildWindowBackground() {
        return new ColorDrawable(0xffffffff);
    }

    @Override
    protected View buildContentView() {
        return LayoutInflater.from(getContext()).inflate(R.layout.activity_rx_java, null);
    }
    class Test{
        int age;
        String name;

        public Test(int age, String name) {
            this.age = age;
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "Test{" +
                    "age=" + age +
                    ", name='" + name + '\'' +
                    '}';
        }
    }
    @Override
    protected void initViews() {
        Logger.i("initViews: ");
        /**
         * 被观察者
         * */
        Observable observable = Observable.create(new Observable.OnSubscribe<Test>() {
            @Override
            public void call(Subscriber<? super Test> subscriber) {
                Logger.i("call: ");
                Toast.makeText(getContext(),"测试",Toast.LENGTH_SHORT).show();
                subscriber.onNext(new Test(21,"小明"));
                subscriber.onNext(new Test(22,"小明"));
                subscriber.onNext(new Test(23,"123"));
                subscriber.onCompleted();
            }
        });
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    /**
     * 观察者
     */
    Observer<String> observer = new Observer<String>() {
        @Override
        public void onNext(String s) {
            Logger.i("Item: " + s);
        }

        @Override
        public void onCompleted() {
            Logger.i("Completed!");
        }

        @Override
        public void onError(Throwable e) {
            Logger.i("Error!");
        }
    };
    /**
     * 订阅者
     */
    Subscriber<Test> subscriber = new Subscriber<Test>() {

        @Override
        public void onNext(Test s) {
            Logger.i("Item: " + s.toString());
            Toast.makeText(getContext(),"测试",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCompleted() {
            Logger.i("Completed!");
        }

        @Override
        public void onError(Throwable e) {
            Logger.i("Error!"+e.getMessage());
            Toast.makeText(getContext(),"测试",Toast.LENGTH_SHORT).show();
        }
    };
}
